package io.github.bigbird0101.datatransfer.utils;

import org.apache.dolphinscheduler.spi.enums.DbType;
import org.apache.dolphinscheduler.spi.enums.StorageType;

import static io.github.bigbird0101.datatransfer.constants.Constant.*;

public class ReaderWriterPluginUtils {


    public static String getReaderPluginName(String dbType) {
        switch (dbType) {
            case DATASOURCE_MYSQL:
            case DATASOURCE_TIDB:
            case DATASOURCE_STARROCKS:
            case DATASOURCE_APACHEDORIS:
                return READER_PLUGIN_MYSQL;
            case DATASOURCE_POSTGRESQL:
            case DATASOURCE_OPENGAUSS:
                return READER_PLUGIN_POSTGRESQL;
            case DATASOURCE_ORACLE:
                return READER_PLUGIN_ORACLE;
            case DATASOURCE_SQLSERVER:
                return READER_PLUGIN_SQLSERVER;
            case DATASOURCE_CLICKHOUSE:
                return READER_PLUGIN_CLICKHOUSE;
            case DATASOURCE_DAMENG:
                return READER_PLUGIN_RDBMS;
            case DATASOURCE_KAFKA:
            case DATASOURCE_RABBIT:
            case DATASOURCE_ACTIVE:
            case DATASOURCE_ROCKET:
                return READER_PLUGIN_MQ;
            case DATASOURCE_HTTP:
            case DATASOURCE_HTTPS:
            case DATASOURCE_HTTP_SERVER:
            case DATASOURCE_WEBSOCKET:
                return READER_PLUGIN_API;
            default:
                return tryRdbmsReader(dbType);
        }
    }


    public static String getWriterPluginName(String dbType) {
        switch (dbType) {
            case DATASOURCE_MYSQL:
            case DATASOURCE_TIDB:
            case DATASOURCE_APACHEDORIS:
            case DATASOURCE_STARROCKS:
                return WRITER_PLUGIN_MYSQL;
            case DATASOURCE_POSTGRESQL:
            case DATASOURCE_OPENGAUSS:
            case DATASOURCE_GAUSSDB:
                return WRITER_PLUGIN_POSTGRESQL;
            case DATASOURCE_ORACLE:
                return WRITER_PLUGIN_ORACLE;
            case DATASOURCE_SQLSERVER:
                return WRITER_PLUGIN_SQLSERVER;
            case DATASOURCE_CLICKHOUSE:
                return WRITER_PLUGIN_CLICKHOUSE;
            case DATASOURCE_DAMENG:
                return WRITER_PLUGIN_RDBMS;
            case DATASOURCE_KAFKA:
                return WRITER_PLUGIN_KAFKA;
            case DATASOURCE_ELASTICSEARCH:
                return WRITER_PLUGIN_ELASTICSEARC;
            default:
                return tryRdbmsWriter(dbType);
        }
    }

    private static String tryRdbmsReader(String dbType) {
//        // rdbms和mpp和数仓存储都尝试使用通用的rdbms reader
        DbType type = DbType.valueOf(dbType);
        if (type == null) {
            return null;
        }
        StorageType storageType = type.getStorageType();
        if (storageType == StorageType.MPP || storageType == StorageType.RDBMS || storageType == StorageType.DATA_WAREHOUSE) {
            return READER_PLUGIN_RDBMS;
        }
        return null;
    }

    private static String tryRdbmsWriter(String dbType) {
//        // rdbms和mpp和数仓存储都尝试使用通用的rdbms writer
        DbType type = DbType.valueOf(dbType);
        if (type == null) {
            return null;
        }
        StorageType storageType = type.getStorageType();
        if (storageType == StorageType.MPP || storageType == StorageType.RDBMS || storageType == StorageType.DATA_WAREHOUSE) {
            return WRITER_PLUGIN_RDBMS;
        }
        return null;
    }
}
